﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;

namespace ProgNetComponents.Design.Editors
{
    public class ObjectArrayEditor : UITypeEditor
    {
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            Type t = null;
            if (context != null && context.Instance != null)
            {
                if (context.PropertyDescriptor != null)
                {
                    foreach (var a in context.PropertyDescriptor.Attributes)
                    {
                        if (a is ProgNetComponents.Design.Attributes.ItemTypePropertyAttribute)
                        {
                            object o = context.Instance.GetPropertyValue((a as ProgNetComponents.Design.Attributes.ItemTypePropertyAttribute).ItemTypeProperty);
                            if (o != null && o is Type)
                            {
                                t = o as Type;
                            }
                            break;
                        }
                    }
                }
            }
            using (Forms.ObjectArrayEditorForm f = new Forms.ObjectArrayEditorForm((object[])value, t))
            {
                if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    value = f.EditValues;
                }
            }
            return value;
        }
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}